package com.cw.jeeyt.service.impl.git;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.git.GitUserBranchBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.git.GitUserBranchDao;
import com.cw.jeeyt.dao.model.git.GitUserBranchDo;
import com.cw.jeeyt.service.intf.git.GitUserBranchService;
import com.cw.jeeyt.service.converter.git.GitUserBranchConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 用户对应项目分支业务实现类
 *
 * @author chenw
 * @date 2019-02-25 01:04:34
 */
@Service
public class GitUserBranchServiceImpl implements GitUserBranchService {

    private static final Logger logger = LoggerFactory.getLogger(GitUserBranchServiceImpl.class);
    @Autowired
    private GitUserBranchDao gitUserBranchDao;//用户对应项目分支数据库操作接口

    /**
     * 查询所有用户对应项目分支
     */
    @Override
    public List<GitUserBranchBo> queryGitUserBranchList() throws Exception {
        return GitUserBranchConverter.doList2BoList(gitUserBranchDao.queryGitUserBranchList());
    }

    /**
     * 分页查询用户对应项目分支
     *
     * @param gitUserBranchBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<GitUserBranchBo> queryByPage(GitUserBranchBo gitUserBranchBo, Integer startNum, Integer pageSize,
        Order order) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return null;
        }
        PageInfo<GitUserBranchDo> pageInfo = gitUserBranchDao.queryByPage(GitUserBranchConverter.bo2Do(gitUserBranchBo),
            startNum, pageSize, order);
        return new PageInfo<>(pageInfo, GitUserBranchConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询用户对应项目分支信息
     *
     * @return
     */
    @Override
    public GitUserBranchBo getGitUserBranchById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return GitUserBranchConverter.do2Bo(gitUserBranchDao.getById(id));
    }

    /**
     * 新增用户对应项目分支
     *
     * @param gitUserBranchBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(GitUserBranchBo gitUserBranchBo) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUserBranchDo gitUserBranch = GitUserBranchConverter.bo2Do(gitUserBranchBo);
        gitUserBranch.setCreateBy(userBo.getId());
        gitUserBranch.setCreateDate(new Date());
        gitUserBranch.setUpdateBy(userBo.getId());
        gitUserBranch.setUpdateDate(new Date());
        Integer result = gitUserBranchDao.insert(gitUserBranch);
        return result;
    }

    /**
     * 新增用户对应项目分支不为空的字段
     *
     * @param gitUserBranchBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(GitUserBranchBo gitUserBranchBo) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUserBranchDo gitUserBranch = GitUserBranchConverter.bo2Do(gitUserBranchBo);
        gitUserBranch.setCreateBy(userBo.getId());
        gitUserBranch.setCreateDate(new Date());
        gitUserBranch.setUpdateBy(userBo.getId());
        gitUserBranch.setUpdateDate(new Date());
        Integer result = gitUserBranchDao.insertSelective(gitUserBranch);
        return result;
    }

    /**
     * 更新用户对应项目分支
     *
     * @param gitUserBranchBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(GitUserBranchBo gitUserBranchBo) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUserBranchDo gitUserBranch = GitUserBranchConverter.bo2Do(gitUserBranchBo);
        gitUserBranch.setUpdateBy(userBo.getId());
        gitUserBranch.setUpdateDate(new Date());

        gitUserBranchDao.update(gitUserBranch);
    }

    /**
     * 更新用户对应项目分支不为空的字段
     *
     * @param gitUserBranchBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(GitUserBranchBo gitUserBranchBo) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUserBranchDo gitUserBranch = GitUserBranchConverter.bo2Do(gitUserBranchBo);
        gitUserBranch.setUpdateBy(userBo.getId());
        gitUserBranch.setUpdateDate(new Date());

        gitUserBranchDao.updateSelective(gitUserBranch);
    }

    /**
     * 删除用户对应项目分支
     *
     * @param gitUserBranchBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(GitUserBranchBo gitUserBranchBo) throws Exception {

        if (gitUserBranchBo == null) {
            logger.error("用户对应项目分支为空");
            return;
        }
        GitUserBranchDo gitUserBranch = GitUserBranchConverter.bo2Do(gitUserBranchBo);
        gitUserBranchDao.delete(gitUserBranch);
    }
}
